<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item size="mini" label="" prop="dictName">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item size="mini" label="" prop="dictFeedback">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入内容"
          clearable
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item size="mini" label="" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          style="width: 140px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item size="mini">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" size="small" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column label="编码" align="center" prop="feedbackCode" min-width="70" />
      <el-table-column label="标题" align="center" min-width="90">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            trigger="click"
          >
            <div class="ellipsis-all-content">{{ scope.row.title }}</div>
            <div slot="reference" class="ellipsis">{{ scope.row.title }}</div>
          </el-popover>
        </template>
      </el-table-column>
      <!--<el-table-column label="内容" align="center" min-width="120">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            trigger="click"
          >
            <div class="ellipsis-all-content">{{ scope.row.content }}</div>
            <div slot="reference" class="ellipsis">{{ scope.row.content }}</div>
          </el-popover>
        </template>
      </el-table-column>-->
      <el-table-column label="创建人" align="center" prop="creator" min-width="50" />
      <el-table-column label="联系方式" align="center" prop="phone" min-width="90" />
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="90">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="经办人" align="center" prop="userName" min-width="50" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" min-width="60" />
      <el-table-column label="完成时间" align="center" prop="createTime" min-width="70">
        <template slot-scope="scope">
          <span>{{ scope.row.finishTime | dateFilter('yyyy-mm-dd') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="120">
        <template slot-scope="scope">
          <!--  <el-button
            v-hasPermi="['system:dict:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>-->
          <el-button
            v-if="handleFeedbackBtn(scope.row, 1)"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="goDetail(scope.row, true)"
          >处理
          </el-button>
          <el-button
            v-if="handleFeedbackBtn(scope.row, 2)"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="goDetail(scope.row, true)"
          >确认
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="goDetail(scope.row, false)"
          >详情
          </el-button>
          <el-button
            v-if="user.fid === scope.row.createId"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="form.content" type="textarea" :autosize="{ minRows: 2, maxRows: 6 }" placeholder="请输入用户反馈" />
        </el-form-item>
        <el-form-item label="文件" prop="fileIds">
          <el-upload
            ref="upload"
            class="plan-upload-btn"
            :before-upload="beforeUpload"
            :data="{ type: 'feedback' }"
            name="files"
            :multiple="true"
            accept=".png, .jpg, .jpeg"
            :on-success="handleUploadSuccess"
            list-type="picture-card"
            :auto-upload="false"
            :action="uploadUrl"
            :on-change="fileChange"
            :on-remove="fileChange"
          >
            <i class="el-icon-plus" />
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="uploadFiles">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeedback, delFeedback, addFeedback, updateFeedback } from '@/api/baseManage/feedback'
export default {
  name: 'Feedback',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [
        { dictLabel: '未受理', dictValue: 0 },
        { dictLabel: '已受理', dictValue: 1 },
        { dictLabel: '已处理', dictValue: 2 },
        { dictLabel: '已确认', dictValue: 3 }
      ],
      // 统一文件上传接口
      uploadUrl: process.env.VUE_APP_BASE_API + '/file/multiUpload',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        content: undefined,
        status: undefined
      },
      // 表单参数
      form: {
        title: '',
        content: '',
        fileIds: ''
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '用户反馈不能为空', trigger: 'blur' }
        ]
      },
      user: this.$store.state.user.user,
      count: 0,
      fileList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户反馈列表 */
    getList() {
      this.loading = true
      listFeedback(this.queryParams).then(response => {
        this.typeList = response.rows
        this.total = response.total
        this.loading = false
      }
      )
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.$refs.form.resetFields()
      this.form = {
        title: '',
        content: '',
        fileIds: ''
      }
      this.count = 0
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        title: '',
        content: '',
        status: ''
      }
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加用户反馈'
    },
    /* 上传 */
    beforeUpload(file) {
      const file_type = file.type
      const isLt2M = file.size / 1024 / 1024 < 2

      const accept_type = ['image/jpeg', 'image/png', 'image/jpg']
      const accept = accept_type.includes(file_type)
      if (!accept) {
        this.msgError('上传图片只能是 JPG/PNG 格式!')
      }
      if (!isLt2M) {
        this.msgError('上传图片大小不能超过 2MB!')
      }
      return accept && isLt2M
    },
    fileChange(file, fileList) {
      this.fileList = fileList
    },
    /* 上传成功*/
    handleUploadSuccess(response, file, fileList) {
      if (response.code !== 200) {
        // 上传文件失败 则抛出错误信息
        this.msgError('上传文件失败 ' + response.msg)
        return
      }
      if (this.form.fileIds) {
        this.form.fileIds = this.form.fileIds + ',' + response.data[0]
      } else {
        this.form.fileIds = response.data[0]
      }
      this.count = this.count + 1
      if (this.count === this.fileList.length) {
        this.submitForm()
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dictId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form = { ...row }
      this.open = true
      this.title = '修改用户反馈'
    },
    // 手动上传文件
    uploadFiles() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.fileList.length > 0) {
            this.$refs.upload.submit()
          } else {
            this.submitForm()
          }
        }
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.feedbackId !== undefined) {
            updateFeedback(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
                this.reset()
              }
            })
          } else {
            addFeedback(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
                this.reset()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const feedbackIds = this.ids.length > 0 ? this.ids : [row.feedbackId]
      this.$confirm('是否确认删除操作的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delFeedback(feedbackIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    },
    goDetail(row, isHandle) {
      row = { ...row, isHandle: isHandle }
      this.$router.push({ path: '/baseManage/FeedbackDetail', query: { form: row }})
    },
    // 处理操作按钮
    handleFeedbackBtn(row, type) { // type 1:处理，2：确认
      if (row.status < 2 && this.user.fid !== row.createId) {
        if (type === 1) {
          return true
        }
      } else if (row.status === 2) {
        if (this.user.fid === row.createId) {
          if (type === 2) {
            return true
          }
        }
      }
      return false
    }
  }
}
</script>
